package com.isunland.managesystem.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.ProjectProgressOriginal.ProjectProgressSuper;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.List;

@SuppressLint("ResourceAsColor") public class ProjectProgressListAdapter
    extends ArrayAdapter<ProjectProgressSuper> {

  /*
   * 回调接口，点击详情重新适配
   */
  public CallBack mcallBack;

  public interface CallBack {

    void onRefresh(String stageCode, String pstageCode);
  }

  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView tvContentOneSimpleList;
    public final TextView tvContentTwoSimpleList;
    public final TextView tvTitleThreeSimpleList;
    public final TextView tvContentThreeSimpleList;
    public final TextView tvTitleFourSimpleList;
    public final TextView tvContentFourSimpleList;
    public final TextView tvPlanStatus;
    public final TextView tvStageStatus;

    private ViewHolder(LinearLayout rootView, TextView tvContentOneSimpleList,
        TextView tvContentTwoSimpleList, TextView tvTitleThreeSimpleList,
        TextView tvContentThreeSimpleList, TextView tvTitleFourSimpleList,
        TextView tvContentFourSimpleList, TextView tvPlanStatus, TextView tvStageStatus) {
      this.rootView = rootView;
      this.tvContentOneSimpleList = tvContentOneSimpleList;
      this.tvContentTwoSimpleList = tvContentTwoSimpleList;
      this.tvTitleThreeSimpleList = tvTitleThreeSimpleList;
      this.tvContentThreeSimpleList = tvContentThreeSimpleList;
      this.tvTitleFourSimpleList = tvTitleFourSimpleList;
      this.tvContentFourSimpleList = tvContentFourSimpleList;
      this.tvPlanStatus = tvPlanStatus;
      this.tvStageStatus = tvStageStatus;
    }

    public static ViewHolder create(LinearLayout rootView) {

      TextView tvContentOneSimpleList =
          (TextView) rootView.findViewById(R.id.tv_contentOne_simpleList);
      TextView tvContentTwoSimpleList =
          (TextView) rootView.findViewById(R.id.tv_contentTwo_simpleList);
      TextView tvTitleThreeSimpleList =
          (TextView) rootView.findViewById(R.id.tv_titleThree_simpleList);
      TextView tvContentThreeSimpleList =
          (TextView) rootView.findViewById(R.id.tv_contentThree_simpleList);
      TextView tvTitleFourSimpleList =
          (TextView) rootView.findViewById(R.id.tv_titleFour_simpleList);
      TextView tvContentFourSimpleList =
          (TextView) rootView.findViewById(R.id.tv_contentFour_simpleList);
      TextView tvPlanStatus = (TextView) rootView.findViewById(R.id.tv_planStatus);
      TextView tvStageStatus = (TextView) rootView.findViewById(R.id.tv_stageStatus);

      return new ViewHolder(rootView, tvContentOneSimpleList, tvContentTwoSimpleList,
          tvTitleThreeSimpleList, tvContentThreeSimpleList, tvTitleFourSimpleList,
          tvContentFourSimpleList, tvPlanStatus, tvStageStatus);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_project_regedit, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    ProjectProgressSuper item = getItem(position);

    vh.tvContentOneSimpleList.setText(
        MyStringUtil.connect(item.getRemark(), "  ", item.getStageName()));//阶段
    vh.tvContentTwoSimpleList.setText(MyStringUtil.connect("负责人：",item.getChargeManName()));//负责人
    if (item.getPlanSTime() != null || item.getPlanETime() != null) {
      vh.tvContentThreeSimpleList.setText(
          MyStringUtil.connect(item.getPlanSTime() == null ? "" : item.getPlanSTime(), " ～ ",
              item.getPlanETime() == null ? "" : item.getPlanETime()));//预计时间
    } else {
      vh.tvContentThreeSimpleList.setText("");
    }
    if (item.getActSTime() != null || item.getActETime() != null) {
      vh.tvContentFourSimpleList.setText(
          MyStringUtil.connect(item.getActSTime() == null ? "" : item.getActSTime(), " ～ ",
              item.getActETime() == null ? "" : item.getActETime()));//实际时间
    } else {
      vh.tvContentFourSimpleList.setText("");
    }

    if (item.getChildnum() == null || Integer.parseInt(item.getChildnum()) == 0) {
      vh.tvPlanStatus.setVisibility(View.INVISIBLE);
    } else {
      vh.tvPlanStatus.setText(R.string.child_stage);
      vh.tvPlanStatus.setBackgroundColor(
          getContext().getResources().getColor(R.color.blue_textColor));
      final String stageCode = item.getStageCode();
      final String pstageCode = item.getPstageCode();

      vh.tvPlanStatus.setOnClickListener(new OnClickListener() {

        @Override public void onClick(View v) {
          mcallBack.onRefresh(stageCode, pstageCode);
        }
      });
    }

    if (!TextUtils.isEmpty(item.getActSTime()) || !TextUtils.isEmpty(item.getActETime())) {
      //vh.tvContentOneSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.green));//阶段
      //vh.tvContentTwoSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.green));//负责人
      //vh.tvTitleThreeSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.green));//预计时间
      //vh.tvContentThreeSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.green));//预计时间
      //vh.tvTitleFourSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.green));//实际时间
      //vh.tvContentFourSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.green));//实际时间

      vh.tvStageStatus.setText("已展开");
      vh.tvStageStatus.setTextColor(getContext().getResources().getColor(R.color.green));//阶段状态
    } else {
      //vh.tvContentOneSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.gray_homepage_text));//阶段
      //vh.tvContentTwoSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.gray_homepage_text));//负责人
      //vh.tvTitleThreeSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.gray_homepage_text));//预计时间
      //vh.tvContentThreeSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.gray_homepage_text));//预计时间
      //vh.tvTitleFourSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.gray_homepage_text));//实际时间
      //vh.tvContentFourSimpleList.setTextColor(
      //    getContext().getResources().getColor(R.color.gray_homepage_text));//实际时间

      vh.tvStageStatus.setText("未展开");
      vh.tvStageStatus.setTextColor(getContext().getResources().getColor(R.color.red));//阶段状态
    }

    return vh.rootView;
  }

  private LayoutInflater mInflater;

  // Constructors
  public ProjectProgressListAdapter(Context context, List<ProjectProgressSuper> objects,
      CallBack callBacK) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mcallBack = callBacK;
  }
}

